/*
 * Copyright 2012 Benoît Combe <benoit.combe@gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.ipst.spot.formulaires.entites.utils;

import fr.ipst.spot.formulaires.FormAbstractHtml;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Benoît Combe <benoit.combe@gmail.com>
 */
public class FormDateBeforeAfter extends FormAbstractHtml {

    private static final long serialVersionUID = 1L;
    private String after;
    private String before;
    private int locale;
    private SimpleDateFormat dateFormater;
    private Date beforeDate = GregorianCalendar.getInstance().getTime();
    private Date afterDate = new GregorianCalendar(1999, 1, 1).getTime();

    /**
     *
     */
    public FormDateBeforeAfter() {
        this.dateFormater = new SimpleDateFormat("dd/MM/yyyy");
    }

    /**
     *
     * @param request
     */
    public FormDateBeforeAfter(HttpServletRequest request) {
        this();
        this.after = request.getParameter("after");
        this.before = request.getParameter("before");
        try {
            if (this.after != null && !this.after.isEmpty()) {
                this.afterDate = this.dateFormater.parse(after);
            }
            if (this.before != null && !this.before.isEmpty()) {
                this.beforeDate = this.dateFormater.parse(before);
            }
        } catch (ParseException e) {
        }
        request.setAttribute("after", this.after);
        request.setAttribute("before", this.before);
    }

    /**
     *
     * @return
     */
    public String getAfter() {
        return after;
    }

    /**
     *
     * @param after
     */
    public void setAfter(String after) {
        this.after = after;
    }

    /**
     *
     * @return
     */
    public String getBefore() {
        return before;
    }

    /**
     *
     * @param before
     */
    public void setBefore(String before) {
        this.before = before;
    }

    /**
     *
     * @return
     */
    public Date getAfterDate() {
        return this.afterDate;
    }

    /**
     *
     * @return
     */
    public Date getBeforeDate() {
        return this.beforeDate;
    }

    /**
     *
     * @return
     */
    public boolean isValid() {
        return (this.after != null && !this.after.isEmpty()) || (this.before != null && !this.before.isEmpty());
    }
}
